package com.pangolin.pangolinim.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.net.NetFormat;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.pangolin.pangolinim.utils.SPUtil;
import com.pangolin.pangolinim.utils.UIUtil;

import java.util.HashMap;
import java.util.Map;

public class ChangeNameActivity extends BaseActivity {
    private UIContact me;

    private Button mBtnRename;
    private EditText mEtNewName;

    @Override
    protected void init(){
        String strMe = getIntent().getStringExtra("param1");
        if(strMe != null){
            me = GsonUtil.fromJson(strMe , UIContact.class);
        }
    }

    @Override
    protected void initView(){
        mBtnRename = findViewById(R.id.btnReName);
        mEtNewName = findViewById(R.id.etNewName);

        setToolbarTitle(UIUtil.getString(R.string.repass));
    }

    @Override
    protected void initListener(){
        mBtnRename.setOnClickListener(v -> {
            String newName = mEtNewName.getText().toString();
            if (TextUtils.isEmpty(newName)){
                UIUtil.showToast("请输入新的用户名");
                return;
            }
            Map<String, Object> brocastMap = new HashMap<String, Object>();
            brocastMap.put("type" , BroadcastMsg.DATA_UI_REINFO);
            brocastMap.put("username" , newName);
            brocastMap.put("headimg" , me.getHeader());
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
            BroadcastSender(bundleUtil.getBundle());

        });
    }

    @Override
    protected void CoreMessage(BundleUtil bundleUtil){
        if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_DATA){
            if(bundleUtil.getData() != null){
                Map<String, Object> resMap = bundleUtil.getData();
                if((int) resMap.get("type") == BroadcastMsg.DATA_CORE_REINFO){
                    if((int)resMap.get("result") == 0){
                        UIUtil.showToast("修改成功");
                    }else{
                        UIUtil.showToast(NetFormat.ResMsg((int) resMap.get("result")));
                    }
                }
            }
        }
    }

    @Override
    protected int provideContentViewId(){
        return R.layout.activity_change_name;
    }
}